package com.villaday.idf.adapter2;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.villaday.idf.R;
import com.villaday.idf.adapter.EndLessOnScrollListener;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.model2.Search_type;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tudou on 2018/10/8.
 */

public class HomeTeMaiSearchAdapter<T> extends AbsRecyclerViewAdapter<Search_type.BodyEntity.ItemsEntity> {
    private String data = "";
    private List<Search_type.BodyEntity.ItemsEntity> datas = new ArrayList<>();

    private List<Search_type.BodyEntity.ItemsEntity> fillDatas = new ArrayList<>();//实际填充集合


    public HomeTeMaiSearchAdapter(Context context, RecyclerView recyclerView) {
        super(context, R.layout.item_home_temai_search);
        recyclerView.addOnScrollListener(new EndLessOnScrollListener((LinearLayoutManager) recyclerView.getLayoutManager()) {
            @Override
            public void onLoadMore(int currentPage) {
                if (datas.size()<=0){
                    return;
                }
                getNextPage();
                notifyDataSetChanged();
            }
        });

    }

    public void setSearchData(String Data) {
        data = Data;
    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, final Search_type.BodyEntity.ItemsEntity d, int position) {
        StringBuffer str = new StringBuffer("");
        List<String> input = new ArrayList<>();
        input.add(data);

        holder.bindTextViewWithHtml(R.id.tv_des, TextUtils.isEmpty(data) ? d.getName() : addChild(d.getName(), input, str).toString())
                .bindTextViewWithHtml(R.id.tv_price, "&yen;" + d.getCurrentPriceValue())
        ;
        List<String> tags = new ArrayList<>();
        if (!TextUtils.isEmpty(d.getDiscountValue())) {
            tags.add(d.getDiscountValue() + "折");
        }
        tags.addAll(d.getTags());
        TagFlowLayout tfl = (TagFlowLayout) holder.getView(R.id.fl_history);
        if (!TextUtils.isEmpty(d.getDiscountValue()) || (d.getTags() != null && d.getTags().size() != 0)) {
            TagAdapter<T> tagAdapter = new TagAdapter<T>((List<T>) tags) {
                @Override
                public View getView(FlowLayout parent, int position, T s) {

                    LayoutInflater inflater = LayoutInflater.from(context);
                    TextView tv;
                    if (!TextUtils.isEmpty(d.getDiscountValue()) && position == 0) {
                        tv = (TextView) inflater.inflate(R.layout.tv_home_tag_pink, null);
                    } else {
                        tv = (TextView) inflater.inflate(R.layout.tv_home_tag, null);
                    }
                    if (s instanceof String) {
                        //当地热门

                        String hot = (String) s;
                        if (hot.length() > 10) {
                            tv.setText(hot.substring(0, 6) + "...");

                        } else {
                            tv.setText(hot);

                        }

                    }


                    return tv;
                }
            };
            tfl.setAdapter(tagAdapter);
        }


    }

    /**
     * 多关键字查询表红,避免后面的关键字成为特殊的HTML语言代码
     *
     * @param str    检索结果
     * @param inputs 关键字集合
     * @param resStr 表红后的结果
     */
    public static StringBuffer addChild(String str, List<String> inputs, StringBuffer resStr) {
        int index = str.length();//用来做为标识,判断关键字的下标
        String next = "";//保存str中最先找到的关键字
        for (int i = inputs.size() - 1; i >= 0; i--) {
            String theNext = inputs.get(i);
            int theIndex = str.indexOf(theNext);
            if (theIndex == -1) {//过滤掉无效关键字
                inputs.remove(i);
            } else if (theIndex < index) {
                index = theIndex;//替换下标
                next = theNext;
            }
        }

        //如果条件成立,表示串中已经没有可以被替换的关键字,否则递归处理
        if (index == str.length()) {
            resStr.append(str);
        } else {
            resStr.append(str.substring(0, index));
            resStr.append("<font color='#F73637'>" + str.substring(index, index + next.length()) + "</font>");
            String str1 = str.substring(index + next.length(), str.length());
            addChild(str1, inputs, resStr);//剩余的字符串继续替换
        }
        return resStr;
    }

    @Override
    public void setData(List<Search_type.BodyEntity.ItemsEntity> data) {
        //重新设置数据
        datas = data;
        fillDatas.clear();

        if (data.size() <= 15) {
            fillDatas.addAll(datas);
        } else {
            getNextPage();
        }


        super.setData(fillDatas);
    }

    private void getNextPage() {

        int fsize = fillDatas.size();
        int size = datas.size();


        if (fsize + 15 > size) {
            for (int i = fsize ; i < size; i++) {
                fillDatas.add(datas.get(i));
            }
        } else {
            for (int i = fsize; i < fsize + 15; i++) {
                fillDatas.add(datas.get(i));
            }
        }

    }


}
